package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.client.utils.JSONUtils;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.produck.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;


/**
 * @Description: TODO
 * @Author: Mr.Cao
 * @DateTime: 2021/10/11 20:16
 * BMW F750GS
 **/
@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor; //创建线程池对象
    @Autowired
    private ListFeignClient listFeignClient;
    /**
     * 获取sku详情信息
     */
    public Map<String, Object> getBySkuId(Long skuId){
        Map<String, Object> result = new HashMap<>();
        CompletableFuture<SkuInfo> completableFuture = CompletableFuture.supplyAsync(()->{
            //获取sku
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            result.put("skuInfo",skuInfo);
            return skuInfo;
        },threadPoolExecutor);
        CompletableFuture<Void> categoryView = completableFuture.thenAcceptAsync((skuInfo) -> {
            //获取 一二三级分类
            BaseCategoryView category123 = productFeignClient.getCategory123(skuInfo.getCategory3Id());
            result.put("categoryView", category123);
        }, threadPoolExecutor);
        CompletableFuture<Void> valuesSkuJson = completableFuture.thenAcceptAsync((skuInfo) -> {
            //根据spuId 查询map 集合属性 组合与对应skuId
            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            result.put("valuesSkuJson", JSONObject.toJSONString(skuValueIdsMap));//转换成json字符串
        }, threadPoolExecutor);
        CompletableFuture<Void> spuSaleAttrList = completableFuture.thenAcceptAsync((skuInfo) -> {
            // 查询销售属性及选中的属性
            List<SpuSaleAttr> spuSaleAttrListCheckBySku = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            result.put("spuSaleAttrList", spuSaleAttrListCheckBySku);
        }, threadPoolExecutor);

        completableFuture.runAsync(() -> {
            //获取实时价格
            BigDecimal price = productFeignClient.getPrice(skuId);
            result.put("price", price);
        },threadPoolExecutor);

        completableFuture.runAsync(() -> {
            //加热度值
            listFeignClient.incrHostScore(skuId,1);
        },threadPoolExecutor);
        completableFuture.allOf(categoryView,valuesSkuJson,spuSaleAttrList).join();
        return  result;


    }
    /**
     * 获取sku详情信息 未使用多线程优化
     * */
    public Map<String, Object> getBySkuIdJUC(Long skuId){
        Map<String, Object> result = new HashMap<>();

        //获取sku
        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
        //获取 一二三级分类
        BaseCategoryView category123 = productFeignClient.getCategory123(skuInfo.getCategory3Id());
        //获取实时价格
        BigDecimal price = productFeignClient.getPrice(skuId);
        //根据spuId 查询map 集合属性 组合与对应skuId
        Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
        //*根据spuId，skuId 查询销售属性集合
        // 查询销售属性及选中的属性
        List<SpuSaleAttr> spuSaleAttrListCheckBySku = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
        result.put("skuInfo",skuInfo);
        result.put("categoryView",category123);
        result.put("price",price);
        result.put("valuesSkuJson", JSONObject.toJSONString(skuValueIdsMap));//转换成json字符串
        result.put("spuSaleAttrList",spuSaleAttrListCheckBySku);
        return result;

        //todo 优化: 可以使用多线程处理
    }

}
